//index.js
//获取应用实例
var app = getApp();
Page({
  data: {
    imgUrls: [
      'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
      'http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg',
      'http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg'
    ],
    
    indicatorDots: true,
    showwebview:0,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    window: {},
    pageType:2,
    page: 1,
    page2: 1,
    nodata: 0,
    listData: [],
    listData2: [],
    type: 1,
    tabId: 1,
    year: 0,
    lastpage1: 0,
    lastpage2: 0,
    loading: 0,
    loaded1: 0,
    loaded2: 0,
    notiCount:0,
  },

  onShow:function(){
    console.log("index . onShow");
    //this.bindGetMsg();
   // wx.removeStorageSync("backUrl");
  },
  onLoad: function (options) {
    var that = this;
    //this.canvas();
    var extConfigSync = app.getExtConfigSync();
    app.getUserInfo(function(userinfo){  
      var pageType = 0;
      if (extConfigSync.store_id ==140 || userinfo.tenement_type.company || userinfo.tenement_type.employee){
        pageType = 2;
      } else if (userinfo.tenement_type.house){
        pageType =1 ;
      }else{
        pageType = 3;
      }
       that.data.backPageType = pageType;
      // if (userinfo.wxopenid == '') {
      //   var bindWxOpenidUrl = app.data.apiHost + "index/bindNotiWx?token=" + userinfo.token +"#wechat_redirect";
   
      //   that.setData({ bindWxOpenidUrl: bindWxOpenidUrl });
      //   pageType = 4;
      //   console.log('setTimeout');
      //   setTimeout(function(){
      //     that.bindGetMsg();
      //   },1000)
      // }
     // pageType = 4;
     
      that.setData({ 'user_info': userinfo, 'pageType': pageType, 'community': userinfo.community});
      if (userinfo.community && userinfo.community.community_name) {
        wx.setNavigationBarTitle({
          title: userinfo.community.community_name,
        })
      }
      that.getnotification();
    })
  },

  bindGetMsg:function(){
    if (this.data.backPageType){
      console.log(this.data.backPageType);
      this.setData({ pageType: this.data.backPageType });
    }
    //that.data.backPageType = pageType
    //console.log('123123213');
    // console.log("showwebviewmsg",e);
  },
  onReachBottom: function () {
   // console.log(12);
    //this.getList();
    this.getnotification();
  },
   
  
  swiperChange: function (e) {

    this.setData({ tabId: e.detail.current });
    //this.getList(2);
    
  },
  //下拉刷新
  onPullDownRefresh: function () {
    this.getnotification(1);
  },

  //提醒列表
  getnotification(reload) {
    if (reload==1){
      this.setData({
        loading: 0,
        page2:1,
        listData:[],
        lastPage2:0
      });
    }
    //通知提醒 
    var that = this;
    var page = this.data.page2;
    this.setData({
      loading: 1
    });
    app.api("api/notification/get", {
      page: page,
      status:0,
      limit: 20,
    }, function (res) {
      wx.stopPullDownRefresh();
      if (res.code != 200) {
        app.alert(res.msg);
        return;
      }
      that.setData({ notiCount: res.data.recordsFiltered});
      //that.delNoit("noti");
      if (res.data.list.length == 0) {
        that.setData({
          lastPage2: 1
        });
      }

      page++;
      if (res.data.list) {
        that.setData({ page2: page, loading: 0, 'listData': that.data.listData.concat(res.data.list) });
      }
      if (that.data.listData.length==0){
        that.setData({ showNoData:1});
      }
    });
  },
  changePage:function(e){
    var pageType = this.data.pageType==1?2:1;
    this.setData({ pageType: pageType});
  },
  jumpnoti: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    that.data.listData[id].status = 1;
    var data = this.data.listData[id];
    app.api("api/notification/add", {
      status: 1,
      id: data.id,
      method: 'POST',
    }, function (res) {
      that.setData({ listData: that.data.listData });
    });
    wx.navigateTo({
      url: "/"+data.url
    })
  },


})
